var smartlocks = [];
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
var smartlockAutoLoadTimer = false;
var smartlockAutoLoad = false;


function loadSmartlock(callback) {
    window.loadSmartlockCallBack = callback;
    $.get("rest/smartlocks/findall?operator=" + operator, function(success) {
        var result = JSON.parse(success);
        for (var i = 0; i < smartlocks.length; i++) {
            smartlocks[i].setMap(null);
        }
        for (var i = 0; i < result.length; i++) {
            createSmartlock({
                position: createPoint(result[i]['lat'], result[i]['lng']),
                title: result[i]['reference'],
                abonnement: result[i]['abonnement'],
                actif: result[i]['actif']
            });
        }
        if (window.loadSmartlockCallBack)
            window.loadSmartlockCallBack.call(result);
    });
}

function reloadSmartlocks() {  
    loadSmartlock(load_admin_table_reporting);
}

function autoLoadSmartlock(delay,callback) {
    if (delay < 3000) {
        console.log('Can not set reloading every '+delay+' milliseconds');
        return;
    } 
    console.log('Reloading every '+delay+' milliseconds');
    if (smartlockAutoLoad === true) {
        clearInterval(smartlockAutoLoadTimer);
        smartlockAutoLoad = false;
        callback.call(false);
    } else {
        smartlockAutoLoadTimer = setInterval(function() {
            reloadSmartlocks();
        }, delay);
        callback.call(true); 
        smartlockAutoLoad = true;
    }
}

function saveSmartlock(option, onSuccess) {
    XHRpost((option.id) ? "rest/smartlocks/update" : "rest/smartlocks/create", {
        operator: option.operator,
        lat: option.lat,
        lng: option.lng,
        reference: option.reference,
        abonnement: option.abonnement,
        actif: option.actif,
        id: option.id
    }, onSuccess);
}

function setGreenSmartlock(smartlock) {
    smartlock.setIcon('/supervision/images/bicycle.png');
}

function setRedSmartlock(smartlock) {
    smartlock.setIcon('/supervision/images/bicycle-red.png');
}

function setOrangeSmartlock(smartlock) {
    smartlock.setIcon('/supervision/images/bicycle-orange.png');
}
 
function createSmartlock(option) {
    var marker = new google.maps.Marker({
        position: option.position,
        map: map,
        draggable: false,
        title: option.title,
        mapTypeId: google.maps.MapTypeId.SATELLITE
    });
    if ((option.abonnement == 0 || option.abonnement == null) && option.actif == 1) {
        marker.setTitle(option.title + "(Disponible)");
        setGreenSmartlock(marker);
    } else {
        if (option.abonnement == 0 || option.abonnement == null) {
            marker.setTitle(option.title + "(Inactif)");
            setRedSmartlock(marker);
        } else {
            marker.setTitle(option.title + "(En location abo=" + option.abonnement + ")");
            setOrangeSmartlock(marker);
        }
    }
    smartlocks.push(marker); 
}


function computeClosestSmartlock(origin, mode) {
    if (!origin)
        return;
    var destinations = [];
    for (var i = 0; i < smartlocks.length; i++) {
        var tmp = smartlocks[i];
        destinations.push(tmp.position);
    }

    XHRget("rest/smartlocks/closest",
            "lat=" + origin.lat() + "&lng=" + origin.lng() + "&operator=" + operator + "&mode=" + mode,
            function(data) {
                var result = JSON.parse(data);
                load_itineraire_table_reporting(result)
                if (result[0].smartlock) {
                    drawRoute(origin, createPoint(result[0].smartlock['lat'], result[0].smartlock['lng']));
                }
            }
    )
} 

function drawRoute(origin, destination) {
    var request = {
        origin: origin,
        destination: destination,
        travelMode: google.maps.TravelMode.WALKING
    };
    directionsService.route(request, function(result, status) {
        if (status === google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(result);
        }
    });
}
 
 